package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    @Reference
    private AdminService adminService;
    @Reference
    private RoleService roleService;
    @RequestMapping
    public String index(HttpServletRequest request, Model model){
        //获取请求数据：查询条件和分页数据
        Map<String, Object> filters = super.getFilters(request);
        //调用业务层查询数据
        PageInfo<Admin> page = this.adminService.findPage(filters);
        //页面跳转
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        return "admin/index";
    }
    @RequestMapping("/create")
    public String create(){
        return "admin/create";
    }
    @RequestMapping("/save")
    public String save(Admin admin){
        admin.setHeadUrl("http://47.93.148.192:8080/group1/M00/03/F0/rBHu8mHqbpSAU0jVAAAgiJmKg0o148.jpg");
        this.adminService.insert(admin);
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Integer id, Model model){
        Admin admin = this.adminService.getById(id);
        //model.addAttribute("role","role");
        model.addAttribute("admin",admin);
        return "admin/edit";
    }

    @RequestMapping("/update")
    public String update(Admin admin, Model model){
        Integer n = this.adminService.update(admin);
        model.addAttribute("messagePage","修改用户成功");
        return "common/successPage";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id){
        this.adminService.delete(id);
        return "redirect:/admin";
    }

    @RequestMapping("/uploadShow/{id}")
    public String uploadShow(@PathVariable Long id, Model model){
        model.addAttribute("id",id);
        return "admin/upload";
    }

    @RequestMapping("/upload/{id}")
    public String upload(@PathVariable Long id, MultipartFile file) throws IOException {

        //上传头像到七牛云
        byte[] bytes = file.getBytes();
        String fileName = UUID.randomUUID().toString();
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        fileName += extName;
        QiniuUtil.upload2Qiniu(bytes,fileName);

        Admin admin = new Admin();
        admin.setId(id);
        String headUrl = "http://renohtnpm.hb-bkt.clouddn.com/" + fileName;
        admin.setHeadUrl(headUrl);
        adminService.update(admin);
        return "common/successPage";
    }

    @RequestMapping("/assignShow/{id}")
    public String assignShow(@PathVariable  Long id,Model model){
        Map<String, Object> map = this.roleService.findRoleByAdminId(id);
        model.addAttribute("adminId",id);
        model.addAllAttributes(map);
        return  "admin/assignShow";
    }
    @RequestMapping("/assignRole")
    public String assignRole(Long adminId,Long [] roleIds){
        this.roleService.saveUserRoleRealtionShip(adminId,roleIds);
        return "common/successPage";
    }
}